<?php // handle_status_change.php -- Handles any status changes in labs or printers

// Start by buffering output

ob_start();

// Include important constants and set admin_home (we need to set the variable since file() doesn't interpret constants in pathnames)

require_once("init.php");
$admin_home = ADMIN_HOME;

/* Find out the out-of-order file we're writing to -- we can get this from the POST array; I set 
up the first key of the array to be the filename... */

$oo_filename = key($_POST);

/* Then, see if we have 3 key-value pairs; if we do, that means that we're changing the OO status
to ON, and should write a "1" to the corresponding email file */

$post_size = count($_POST);

if ($post_size == 3) {
	next($_POST);
	$email_filename = key($_POST);
	$fp = fopen ("$admin_home/printers/oo_status/oo_emails/$email_filename", 'w');
	fwrite ($fp, "1\n");
	fclose($fp);

	// Also, send an e-mail to me to indicate that the toner e-mails were turned ON

	$exploded_array = explode('_', $oo_filename);
	$length = count($exploded_array);
	$email_filename_array = array_slice($exploded_array, 0, ($length - 1));
	$email_printer = implode('_', $email_filename_array);
	$email_printer = strtoupper($email_printer);

	// Send an e-mail to multiple recipients

	$to  = 'satherdy@grinnell.edu' . ', ';
	$to .= 'botmille@grinnell.edu';
	mail ('', $to, "Out-of-order notifications were turned ON for $email_printer", 'From:techmap@tcdb.grinnell.edu');
}

/* Otherwise, the TC either unchecked the box (i.e. out-of-order e-mails should be turned off) or the box was 
unchecked to begin with; in either situation, we should send a "0" to the corresponding e-mail file, which is
a bit trickier to get... to be frank, this is an overcomplicated way to do it, and I should look for a better
way soon */

else {
	$exploded_array = explode('_', $oo_filename);
	$length = count($exploded_array);
	$email_filename_array = array_slice($exploded_array, 0, ($length - 1));
	$email_printer = implode('_', $email_filename_array);
	$email_filename = "$email_printer" . '_email';
	$fp = fopen ("$admin_home/printers/oo_status/oo_emails/$email_filename", 'w');
	fwrite ($fp, "0\n");
	fclose($fp);

	// Also, send an e-mail to me to indicate that the toner e-mails were turned OFF

	$email_printer = strtoupper($email_printer);

	// Send an e-mail to multiple recipients

	$to  = 'satherdy@grinnell.edu' . ', ';
	$to .= 'botmille@grinnell.edu';
	mail ($to, 'Campus Technology Map notification', "Out-of-order notifications were turned OFF for $email_printer", 'From:techmap@tcdb.grinnell.edu');
}

// Next, write POST data (i.e. the out-of-order message the TC wrote) to file

$fp = fopen ("$admin_home/printers/status_messages/$oo_filename", 'w');
fwrite ($fp, "{$_POST["$oo_filename"]}");
fclose($fp);

// And send an e-mail with the new message

// Send an e-mail to multiple recipients

$to  = 'satherdy@grinnell.edu' . ', ';
$to .= 'botmille@grinnell.edu';

mail ($to, 'Campus Technology Map notification', "The status message for $oo_filename was changed to \"{$_POST["$oo_filename"]}\"", 'From:techmap@tcdb.grinnell.edu');

// Redirect the TC back to index page

header('Location: http://tcdb.grinnell.edu/map');

// End by flushing output

ob_end_flush();

?>
